#!/usr/bin/env python
import getopt
import logging
import os
import six
import sys
import yaml


import aj
import aj.config
import aj.entry
import aj.log
import aj.plugins


@six.python_2_unicode_compatible
class AjentiConfig(aj.config.BaseConfig):
    def __init__(self, path):
        aj.config.BaseConfig.__init__(self)
        self.data = None
        self.path = os.path.abspath(path)

    def __str__(self):
        return self.path

    def load(self):
        if os.geteuid() == 0:
            os.chmod(self.path, 384)  # 0o600
        self.data = yaml.load(open(self.path))

    def save(self):
        with open(self.path, 'w') as f:
            f.write(yaml.safe_dump(self.data, default_flow_style=False, encoding='utf-8', allow_unicode=True))


def usage():
    print("""
Usage: %s [options]
Options:
    -c, --config <file> - Use given config file instead of default
    -v                  - Debug/verbose logging
    --dev               - Dev mode (recompile resources)
    -d, --daemon        - Run in background (daemon mode)
    --stock-plugins     - Load plugins from PYTHONPATH
    --plugins <dir>     - Load plugins from a directory
    --autologin         - Log in automatically as the running user
    -h, --help          - This help
    """ % sys.argv[0])


if __name__ == '__main__':
    log_level = logging.INFO
    daemonize = False
    dev_mode = False
    debug_mode = False
    config_path = None
    autologin = False
    plugin_providers = []

    try:
        opts, args = getopt.getopt(
            sys.argv[1:],
            'hc:dv',
            ['help', 'config=', 'daemon', 'dev', 'stock-plugins', 'plugins=', 'autologin']
        )
    except getopt.GetoptError as e:
        print(str(e))
        usage()
        sys.exit(2)

    for o, a in opts:
        if o in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif o in ('-v',):
            log_level = logging.DEBUG
            debug_mode = True
        elif o in ('--dev',):
            log_level = logging.DEBUG
            debug_mode = True
            dev_mode = True
        elif o in ('-c', '--config'):
            config_path = a
        elif o in ('-d', '--start'):
            daemonize = True
        elif o in ('--autologin',):
            autologin = True
        elif o == '--stock-plugins':
            plugin_providers.append(aj.plugins.PythonPathPluginProvider())
        elif o == '--plugins':
            plugin_providers.append(aj.plugins.DirectoryPluginProvider(a))

    if not plugin_providers:
        plugin_providers.append(aj.plugins.PythonPathPluginProvider())

    aj.log.init_console(log_level)

    if autologin and not debug_mode:
        logging.error('Autologin is a dangerous option and should be used together with -v')
        sys.exit(1)

    # Find default config file
    if not config_path:
        # Check for config file in /etc/ajenti/config.yml
        if os.path.isfile('/etc/ajenti/config.yml'):
            config_path = '/etc/ajenti/config.yml'
        elif os.path.isfile(os.path.join(sys.path[0], 'config.yml')):
            # Try local config file
            config_path = os.path.join(sys.path[0], 'config.yml')

    if not os.path.exists(config_path):
        logging.error('Config file "%s" not found', config_path)
        sys.exit(1)

    aj.entry.start(
        config=AjentiConfig(config_path),
        dev_mode=dev_mode,
        debug_mode=debug_mode,
        autologin=autologin,
        product_name='ajenti',
        daemonize=daemonize,
        plugin_providers=plugin_providers,
    )
